package org.jsbd.boss.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jodd.util.StringUtil;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.dao.IChannelApkInfoDAO;
import org.jsbd.boss.domian.ChannelApkInfo;
import org.jsbd.boss.domian.ChannelMonitorInfo;
import org.jsbd.boss.domian.ChannelRetentLogInfo;
import org.jsbd.boss.domian.ChannelRetentMonitorInfo;
import org.jsbd.boss.domian.channel.ChannelApkConfig;
import org.jsbd.boss.service.IChannelApkInfoService;
import org.jsbd.boss.service.impl.ChannelRetentLogService;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * 
 * 留存日志查询controller
 * @author <a href="mailto:zhangbinghua@zhexinit.com" >zbh</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/channelretent")
public class ChannelRetentController {

	private Logger LOGGER = LoggerFactory.getLogger(ChannelRetentController.class);

	@Autowired
	private ChannelRetentLogService channelRetentLogService;
	
	@Autowired
	private IChannelApkInfoDAO channelApkInfoDAO;
	
	@Autowired
	private IChannelApkInfoService channelApkInfoService;

	/**
	 * getChannelApkInfo: 获取需要刷量的apk信息，返回给手机模拟器
	 * 
	 * @param request
	 * @param resp
	 */
	@RequestMapping(value = "/retent",  params = "json")
	@ResponseBody
	public Map<String, Object> getChannelRetent(HttpServletRequest request, HttpServletResponse resp,ChannelRetentLogInfo channelRetentLogInfo, Model model) {
		String retenttype = channelRetentLogInfo.getRetenttype();
		String currdate = channelRetentLogInfo.getCurrentDate();
		if(StringUtil.isBlank(currdate)){
			currdate = DateUtil.getCurrentDateStr();
			channelRetentLogInfo.setCurrentDate(currdate);
		}else{
			channelRetentLogInfo.setCurrentDate(currdate.replaceAll("-", ""));
		}
		List<ChannelRetentLogInfo> channelRetentLoglist = null;
		if("1".equals(retenttype)){
			channelRetentLoglist = channelRetentLogService.getChannelRetentNewUserLog(channelRetentLogInfo);
		}
		if("2".equals(retenttype)){
			channelRetentLoglist = channelRetentLogService.getChannelRetentLog(channelRetentLogInfo);
		}if("0".equals(retenttype)||StringUtil.isBlank(retenttype)){
			channelRetentLogInfo.setExetimes(null);
			channelRetentLoglist = channelRetentLogService.getChannelRetentALLLog(channelRetentLogInfo);
		}
		int total = 0;
		if (channelRetentLoglist != null && channelRetentLoglist.size() > 0) {
			for (ChannelRetentLogInfo temp : channelRetentLoglist) {
				ChannelApkInfo channleapkinfo = channelApkInfoDAO.getConfigByMark(temp.getMark());
				if (channleapkinfo != null) {
					temp.setApkName(channleapkinfo.getApkName());
				}else{
					//兼容新系统
					ChannelApkConfig  channelApkConfig = channelApkInfoService.selectChannelApkConfig(temp.getMark());
					if (channelApkConfig != null) {
						temp.setApkName(channelApkConfig.getApkName());
					}
				}
				total+=temp.getTotal();
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", channelRetentLoglist);
		map.put("total", total);
		return map;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String configList(Model model) {
		return "channelretent/list";
	}

	@RequestMapping(value = "/monitor", method = RequestMethod.GET)
	public String monitor(Model model) {
		return "channelretent/monitor";
	}
	
	@RequestMapping(value = "/monitor", params = "json")
	@ResponseBody
	public Map<String, Object> monitor(HttpServletRequest request, HttpServletResponse resp) {
		String time = request.getParameter("time");
		if (StringUtils.isBlank(time)) {
			Calendar calendar = Calendar.getInstance();
			time = new SimpleDateFormat(DateUtil.DATE_PATTEN_YY_MM_DD_HHMMSS).format(calendar.getTime());
		}
		List<ChannelMonitorInfo> monitor = channelRetentLogService.getChannelMonitorInfo(time);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", monitor);
		return map;
	}
	
	@RequestMapping(value = "/retentmonitor", method = RequestMethod.GET)
	public String retentmonitor(Model model) {
		return "channelretent/retentmonitor";
	}
	
	@RequestMapping(value = "/retentmonitor", params = "json")
	@ResponseBody
	public Map<String, Object> retentmonitor(HttpServletRequest request, HttpServletResponse resp) {
		String time = request.getParameter("time");
		if (StringUtils.isBlank(time)) {
			Calendar calendar = Calendar.getInstance();
			time = new SimpleDateFormat(DateUtil.DATE_PATTEN_YYMMDD).format(calendar.getTime());
		}
		time = time.replaceAll("-", "");
		List<ChannelRetentMonitorInfo> monitor = channelRetentLogService.getChannelRetentMonitorInfo(time);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", monitor);
		return map;
	}
}
